package com.example.kkblog.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.kkblog.domain.Star;
import com.example.kkblog.domain.dto.DynamicPreDto;
import com.example.kkblog.domain.query.StarQuery;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author xiaoke
 * @date 2024/5/30
 */
@Mapper
@Repository
public interface StarMapper extends BaseMapper<Star> {
    // 查询收藏动态
    @Select("SELECT a.*,COUNT(c.id) as commentNumber FROM   " +
            "(SELECT d.*,t.`name` as topic_name, u.nickname as nickname, u.avatar as avatar FROM `dynamic` d, `topic` t,`user` u, `star` s   " +
            "WHERE d.id=s.dynamic_id and s.user_id=#{userId} and d.topic_id=t.id and d.user_id=u.id and d.status=0 ORDER BY s.id DESC LIMIT #{pageSize}) as a  " +
            "LEFT JOIN dynamic_comment c  " +
            "ON a.id = c.dynamic_id  " +
            "GROUP BY a.id ")
    List<DynamicPreDto> selectStarDynamics(StarQuery starQuery);

    // 查询收藏动态
    @Select("SELECT a.*,COUNT(c.id) as commentNumber FROM   " +
            "(SELECT d.*,t.`name` as topic_name, u.nickname as nickname, u.avatar as avatar FROM `dynamic` d, `topic` t,`user` u, `star` s   " +
            "WHERE d.id=s.dynamic_id and s.user_id=#{userId} and d.topic_id=t.id and d.user_id=u.id and d.status=0 and s.id<#{lastId} ORDER BY s.id DESC LIMIT #{pageSize}) as a  " +
            "LEFT JOIN dynamic_comment c  " +
            "ON a.id = c.dynamic_id  " +
            "GROUP BY a.id ")
    List<DynamicPreDto> selectMoreStarDynamics(StarQuery starQuery);
}
